#coding=utf-8

import sys
from debug import logprint,logger
import dm_model

def get_latest_rank(rankdate):
    latest_rank=[]
    
    
    return latest_rank
    

def indexpage():
    fundinfo=dm_model.webget_info_from_pool()
    #print fundinfo
    latest_rank=[]
    all_rank=[]
    for fundi in fundinfo:
        #print fundi
        code=fundi[0]
        fname=fundi[1]
        rankdate=dm_model.webget_rank_bycode(code,15)
        if rankdate:
            sfund={'code':code,
                   'name':fname,
                   'date':rankdate[0][0],
                   'rank':rankdate[0][1],}
            latest_rank.append(sfund)
    
            rank_list=[]
            for rank in rankdate:
                rank_list.append(rank)
            
            code_rank={'code':code,
                       'rank_list':rank_list,}
            all_rank.append(code_rank)
            
    
    print latest_rank
    print all_rank
    return [latest_rank,all_rank]

def get_fund_pm(code,num,type):
    pms=dm_model.webget_pm_bycode(code,num,type)
    pmdict={}
    if pms:
        for pm in sorted(pms):
            pmdict[pm[0]]=pm[1]
    return pmdict

def get_fund_ranks(code,num):
    rankdate=dm_model.webget_rank_bycode(code,num)
    sdict={}
    if rankdate:
        for rd in rankdate:
            sdict[rd[0]]=rd[1]
    
    return sdict

###### for local script####
if __name__ == "__main__":
    indexpage()
    
    